export const img2Base64Code = `
export const img2Base64 = async (source) => {
    return new Promise((resolve, reject) => {
        // 如果source是文件对象
        if (source instanceof File) {
            const reader = new FileReader();
            reader.onloadend = () => resolve(reader.result);
            reader.onerror = reject;
            reader.readAsDataURL(source);
        }
        // 如果source是字符串，则认为是图片URL
        else if (typeof source === 'string') {
            fetch(source)
                .then(response => {
                    if (!response.ok) throw new Error('网络请求失败');
                    return response.blob();
                })
                .then(blob => {
                    const reader = new FileReader();
                    reader.onloadend = () => resolve(reader.result);
                    reader.onerror = reject;
                    reader.readAsDataURL(blob);
                })
                .catch(reject);
        } else {
            reject('不支持的源类型');
        }
    });
}
const clickPaste = async () => {
// 请求剪切板读取权限
  const permission = await navigator.permissions.query({ name: 'clipboard-read' });
  if (permission.state === 'granted' || permission.state === 'prompt') {
    const items = await navigator.clipboard.read();
    const imageItem = items.find(item => item.types.includes('image/png') ||
        item.types.includes('image/jpeg') ||
        item.types.includes('image/gif') ||
        item.types.includes('image/jpg') ||
        item.types.includes('image/webp'));
    if (!imageItem) {
      window.$message.error('剪切板无图片');
      return;
    }
    let blob;
    if (imageItem.types[0].startsWith('image/')) {
      blob = await imageItem.getType(imageItem.types[0]);
      state.imgUrl = URL.createObjectURL(blob);
    } else {
      throw new Error('不支持的图片类型');
    }
    blob2Base64(blob).then(result => {
      emit('change', result);
    });
  } else {
    window.$message.error('无权限读取剪切板');
  }
}

const blob2Base64 = (blob) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => {
      resolve(reader.result); // 包含 data URL (base64)
    };
    reader.onerror = reject;
    reader.readAsDataURL(blob);
  });
}
`;
